package model.vo
{
	import model.context.StartEleBoundaryProtocalContext;

	/**
	 * "action=interface-sendCmd&id=xxx&tid=xxxx&lx=1" +
				"&M=3&D=N&minlatitude=****.***&maxlatitude=****.***" +
				"&G=E&minlongitude=****.***&maxlongitude=****.***";
	 *
	 **/
	public class ProtocalEleBoundary
	{
		public static var NAME : String = "ProtocalEleBoundary";
		
		public static var DRIVE_IN_ALERT:String="DRIVE_IN_ALERT";
		public static var DRIVE_OUT_ALERT:String="DRIVE_OUT_ALERT";
		public static var SET_BOUNDARY:String="SET_BOUNDARY";
		public static var CANCEL_BOUNDARY:String="CANCEL_BOUNDARY";
		
		//创建边框标志位
		public static var SET_ELE_BUNDARY_LX : String = "1";
		//取消边框标志位
		public static var CANCEL_ELE_BUNDARY_LX : String = "0";
		
		//驶入
		public static var DRIVE_IN_M : String = "3";
		//驶出
		public static var DRIVE_OUT_M : String = "1";
		public static var DEFAULT_M : String = "1";
		
		/**经纬度**/
		//经度
		public static var MIN_LNG_G : String = "1";
		public static var MAX_LNG_G : String = "1";
		
		//维度
		public static var MIN_LAT_G : String = "1";
		public static var MAX_LAT_G : String = "1";
		
		public var action:String ;
		public var id:String = "20";
		public var tid:String = "20";

		
		/**
		 *  设置基本信息
		 **/
		public function setBaseInfo( id:String, tid:String ):void
		{
			this.id = id;
			this.tid = id;
		}
		
		/**
		 * 创建驶入协议
		 **/
		private function createDriveIn( sLatlng:String, eLatlng:String):String
		{
			return createBaseStr( SET_ELE_BUNDARY_LX, DRIVE_IN_M, sLatlng, eLatlng);
		}
		
		/**
		 *  通过上下文创建
		 **/
		public function createByContext(context : StartEleBoundaryProtocalContext):String
		{
			var startlatlngStr : String = context.startLatlnt.lat() + "," + context.startLatlnt.lng();
			var endlatlngStr : String = context.endLatlnt.lat()+ "," + context.endLatlnt.lng();
			
			return create(context.protocalType, startlatlngStr , endlatlngStr);
		}
		
		/**
		 *  创建协议
		 **/
		public function create(type:String,  sLatlng:String, eLatlng:String ) : String
		{
			var outStr : String  = null;
			if ( type == DRIVE_IN_ALERT ) {
				outStr = createDriveIn(sLatlng, eLatlng);
			}
			else if ( type == DRIVE_OUT_ALERT )
			{
				outStr = createDriveOut(sLatlng, eLatlng);
			}
			else if ( type == SET_BOUNDARY )
			{
				outStr = createDriveOut(sLatlng, eLatlng);
			}
			else if ( type == CANCEL_BOUNDARY )
			{
				outStr = createDriveOut(sLatlng, eLatlng);
			}
			return outStr;
		}
		
		/**
		 *  创建驶出协议
		 **/
		private function createDriveOut( sLatlng:String, eLatlng:String ):String
		{
			return createBaseStr( SET_ELE_BUNDARY_LX, DRIVE_OUT_M, sLatlng, eLatlng);
		}
		
		/**
		 *  创建电子围栏.
		 **/
		private function createEleBoundary( sLatlng:String, eLatlng:String ):String
		{
			return createBaseStr( SET_ELE_BUNDARY_LX, DEFAULT_M, sLatlng, eLatlng);
		}
		
		/**
		 * 取消电子围栏.
		 **/
		private function createCancelBoundary(sLatlng:String, eLatlng:String ):String
		{
			return createBaseStr( CANCEL_ELE_BUNDARY_LX, DEFAULT_M, sLatlng, eLatlng);
		}
		
		/**
		 * 创建基本字符
		 **/
		private function createBaseStr(lx:String, m:String, maxLatlng:String, minLatlng:String):String
		{
			var outStr:String = null;
			if ( (maxLatlng == null && maxLatlng.split(",").length!=2) 
				 ||  (maxLatlng == null && maxLatlng.split(",").length!=2) )
			{
				outStr = null;
			}
			else
			{
				var maxLatlngList:Array = maxLatlng.split(",");
				var minLatlngList:Array = maxLatlng.split(",");
				
				var minLat:String = minLatlngList[0] ;
				var maxLat:String =  maxLatlngList[0];
				var minLng:String =  minLatlngList[1];
				var maxLng:String =  maxLatlngList[1];
				
				outStr =  "action=interface-sendCmd&id=" + this.id
							+"&tid="+ this.tid
							+"&lx="+lx
							+"&M="+m
							+"&D=N&minlatitude=" + minLat
							+ "&maxlatitude=" + maxLat
							+"&G=E&minlongitude=" + minLng
							+ "&maxlongitude=" + maxLng;
			}
			
			return outStr;
		}
	}
}